package com.ysu.shop.client;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysu.shop.pojo.Category;
import com.ysu.shop.pojo.Goods;
import com.ysu.shop.pojo.User;
import com.ysu.shop.service.ICategoryService;
import com.ysu.shop.service.IGoodsService;
import com.ysu.shop.service.IUserService;
import com.ysu.shop.utils.InitUtil;
import com.ysu.shop.utils.Result;
import com.ysu.shop.utils.ShaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/shop")
@RestController
@Slf4j
public class indexController {

    @Resource
    private ICategoryService categoryService;

    @Resource
    private IGoodsService goodsService;

    @Resource
    private IUserService userService;

    @GetMapping("/category")
    public Result getListByTree() {
        return Result.getSuccessResult(categoryService.listByTree());
    }

    @GetMapping("/goods")
    public Result getGoods(){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("active",1).eq("parent_id",0);
        List<Category> categories = categoryService.list(wrapper);
        List<JSONObject> result = new ArrayList<>();
        for (Category i:categories) {
            wrapper.clear();
            wrapper.eq("active",1).eq("parent_id",i.getId());
            JSONObject resultItem = new JSONObject();
            resultItem.put("name",i.getName());
            List<Category> categories1 = categoryService.list(wrapper);
            String ids  = categories1.stream().map(j->j.getId().toString()).collect(Collectors.joining(","));
            log.info("ids: {}",ids);
            List<Map<String, Object>> goods = goodsService.getIndexGood(ids);
            if (goods!=null&&goods.size()==6){
                resultItem.put("goods",goods);
                result.add(resultItem);
            }
        }
        return Result.getSuccessResult(result);
    }



    /**
     * 向表中添加一条记录
     */
    @PostMapping("/user/register")
    public Result save(@RequestBody User user) {
        user.setRoleId(4);
        if (StringUtils.isEmpty(user.getPassword())){
            user.setPassword(ShaUtil.getSHA256("123456"));
        } else {
            user.setPassword(ShaUtil.getSHA256(user.getPassword()));
        }
        if (userService.save(user)){
            return Result.getSuccessResult("","注册成功");
        }
        return Result.getFailResult("","注册失败");
    }


    /**
     * 商品搜索
     * @param param
     * @return
     */
    @GetMapping("/goods/search")
    public Result getList(@RequestParam Map<String, Object> param) {
        int num = 1;
        if (!StringUtils.isEmpty(param.get("page"))){
            num = Integer.parseInt(param.get("page").toString());
        }
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("active",1);
        Object name = param.get("name");
        if (!StringUtils.isEmpty(name)) {

            wrapper.like("name",name).or().like("detail",name);
        }
        IPage<Goods> page = new Page<>(num, 8);
        return Result.getSuccessResult(goodsService.page(page, wrapper));
    }

    /**
     * 通过主键id在表中查找信息
     */
    @GetMapping("/goods/{id}")
    public Result getById(@PathVariable int id) {
        Goods iGoods = goodsService.getById(id);
        if (iGoods == null) {
            return Result.getFailResult("","无记录");
        }
        return Result.getSuccessResult(iGoods);
    }
    
}
